﻿@model OES.Models.Question
@{
    ViewBag.Title = "Soru Ekle";//@Html.Hidden("ExamId", new { value = ViewBag.ExamId })
}
@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <input type="hidden" id="ExamId" name="ExamId" value="@ViewBag.ExamId" />
    <div style="float: left">@Html.LabelFor(model => model.Question_TypeId)
    </div>
    <div style="float: left; padding-left: 10px;">
        @Html.DropDownList("Question_TypeId", String.Empty)
        @Html.ValidationMessageFor(model => model.Question_TypeId)
    </div>
        
    <div class="editor-label" style="clear: both;">
        @Html.LabelFor(model => model.Question_Text)
    </div>
    <div class="editor-field">
        <textarea class="ckeditor" cols="80" id="Question_Text" name="Question_Text" rows="10">
			</textarea>
        @Html.ValidationMessageFor(model => model.Question_Text)
    </div>
    <table id="ChoicesTable">
    </table>
    <p>
        <input type="submit" value="Soruyu Ekle" class="btn btn-success" />
    </p>
}
@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
    @Scripts.Render("~/ckeditor/ckeditor.js")
    <script type="text/javascript">
        $(document).ready(function () {
            $("#Question_TypeId").change(function () {
                RemoveChoices();
                var questionType = $("#Question_TypeId").val();
                if (questionType == 1) {
                    for (var i = 0; i < 5; i++)
                        AddNewItem("radio", "RightChoice", i, "");
                }
                else if (questionType == 2) {
                    for (var j = 0; j < 5; j++)
                        AddNewItem("checkbox", "RightChoice", j, "");
                }
                else if(questionType==3) {
                    AddNewItem("radio", "RightChoice", 0, "Doğru");
                    AddNewItem("radio", "RightChoice", 1, "Yanlış");
                }

                else
                RemoveChoices
            });
        });

        function AddNewItem(inputType, id, valueRB, valueTB) {
            var table = document.getElementById("ChoicesTable");
            var rowCount = table.rows.length;
            var row = table.insertRow(rowCount);

            var cell1 = row.insertCell(0);
            var cell2 = row.insertCell(1);
            var newRadioButton = document.createElement('input');

            newRadioButton.setAttribute("type", inputType);
            newRadioButton.setAttribute("value", valueRB);
            newRadioButton.setAttribute("name", id);
            newRadioButton.setAttribute("id", id);


            var newTxtbx = document.createElement('input');
            newTxtbx.setAttribute("type", "text");
            newTxtbx.setAttribute("value", valueTB);
            newTxtbx.setAttribute("name", "ChoiceText");
            newTxtbx.setAttribute("id", "ChoiceText");
            if (valueTB == "Doğru" || valueTB == "Yanlış")
                newTxtbx.setAttribute("readonly", "true");


            cell1.appendChild(newRadioButton);
            cell2.appendChild(newTxtbx);

        }

        function RemoveChoices() {
            try {
                var table = document.getElementById("ChoicesTable");
                var rowCount = table.rows.length;
                for (var i = rowCount - 1; i >= 0; i--)
                    table.deleteRow(i);
            }
            catch (Error) {
                alert(Error);
            }
        }
        
        
    </script>
}
